package com.mlm.mlmapp.adapter.view.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.util.Constants;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.listener.HairStyleClickListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HairStyleAdapter extends
		RecyclerView.Adapter<RecyclerView.ViewHolder> implements HTTPCons {
	private AQuery aq;
	private static final int TYPER_PAGER = 0;
	private static final int TYPE_ITEM = 1;

	private static final int HEADER_PAGER_LAYOUT = 1;

	private Context mContext;
	private JSONObject object;
	private HairStyleClickListener mOnHairStyleClickListener;
	private String id;
	private JSONArray photoArray;
	private String mainPhoto;
	private String intro;
	private String dresserName;
	private String dresserPost;
	private int dresserRating;
	private int dresserCollection;
	private String dresserHeadimg;
	private int dresserOrder;
	private String stylistID;
	private int isCollected;
	private String dresserIntro;
	private int price;
	private JSONObject coupon;
	private ImageView collectImageView;
	String hairurl ;
	public HairStyleAdapter(Context context, JSONObject object) {
		this.object = object;
		this.mContext = context;
		dresserName = CommonUtils.getJsonString(object, "name");
		intro = CommonUtils.getJsonString(object, "infoDescription");
		mainPhoto = CommonUtils.getJsonString(object, "mainPhoto");
		isCollected = CommonUtils.getJsonInt(object, "isCollect");
		stylistID = CommonUtils.getJsonString(object, "stylistId");
		dresserHeadimg = CommonUtils.getJsonString(object, "iconPhotoUrl");
		dresserIntro = CommonUtils.getJsonString(object, "introduce");
		dresserCollection = CommonUtils.getJsonInt(object, "collected");
		dresserRating = CommonUtils.getJsonInt(object, "starLevel");
		dresserPost = CommonUtils.getJsonString(object, "leveName");
		price = CommonUtils.getJsonInt(object, "priceMast");
		coupon = CommonUtils.getJsonObject(object, "coupon");
		photoArray = CommonUtils.getJsonArray(object, "photos");
		dresserOrder = CommonUtils.getJsonInt(object, "bought");

		aq = new AQuery(mContext);

		mOnHairStyleClickListener = (HairStyleClickListener) mContext;
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
			int viewType) {

		if (viewType == TYPER_PAGER) {

			View v = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.hair_style, parent, false);
			final HeaderViewHolder headerHolder = new HeaderViewHolder(v);
			return headerHolder;
		} else if (viewType == TYPE_ITEM) {
			View v = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.hairstyle_detail_viewitem, parent, false);
			return new RecyclerViewHolder(v);
		}

		return null;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

		if (holder instanceof HeaderViewHolder) {
			HeaderViewHolder hHolder = (HeaderViewHolder) holder;
			hHolder.tv_style_intro.setText(this.intro);
			hHolder.tv_name.setText(this.dresserName);
			hHolder.tv_style_intro.setText(this.intro);
			hHolder.tv_post.setText(this.dresserPost);
			hHolder.tv_words.setText(this.dresserIntro);
			hHolder.tv_collections.setText(mContext.getString(
					R.string.txt_collection, this.dresserCollection));
			hHolder.tv_orders.setText(mContext.getString(R.string.txt_order,
					this.dresserOrder));
			hHolder.tv_price.setText(this.mContext.getString(
					R.string.txt_price, this.price));
			// hHolder.ratingBar1.setRating(dresserRating);

			if (this.isCollected == 1) {
				hHolder.iv_collection.setImageResource(R.drawable.collected);
			} else {
				hHolder.iv_collection
						.setImageResource(R.drawable.home_collection);
			}
			this.collectImageView = hHolder.iv_collection;
			aq.id(hHolder.iv_hairstyle)
					.image(mainPhoto, true, true,
							200, 0, null,
							Constants.FADE_IN);
			aq.id(hHolder.iv_headimg).image(dresserHeadimg, true, true, 200, 0,
					null, Constants.FADE_IN);
			hHolder.iv_headimg.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mOnHairStyleClickListener.onDresserClick(stylistID);
				}
			});
			// hHolder.btn_coupon.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// try {
			// CouponGetListActivity_.intent(mContext).extra("couponid",
			// coupon.getString("id")).start();
			//
			// } catch (JSONException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			//
			// }
			// });
			// try {
			// if(coupon.isNull("couponName")){
			// hHolder.btn_coupon.setVisibility(View.INVISIBLE);
			// }else{
			// hHolder.btn_coupon.setText(this.coupon.getString("couponName"));
			// }
			//
			// } catch (JSONException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }

		} else if (holder instanceof RecyclerViewHolder) {
			RecyclerViewHolder rHolder = (RecyclerViewHolder) holder;

			if (photoArray.length() > 0) {
				try {
					hairurl = CommonUtils.getJsonString(
							photoArray.getJSONObject(position
									- HEADER_PAGER_LAYOUT), "photoUrl");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			aq.id(rHolder.iv_hairstyle).image(hairurl, true, true, 200, 0, null,
					Constants.FADE_IN);

		}

	}

	@Override
	public int getItemViewType(int position) {
		if (isPositionHeader(position)) {
			return TYPER_PAGER;
		}
		return TYPE_ITEM;
	}

	public boolean isPositionHeader(int position) {
		return position == 0;
	}

	@Override
	public int getItemCount() {
		return photoArray.length() + HEADER_PAGER_LAYOUT;
	}

	public void setCollectStatus(int status) {
		this.isCollected = status;
		if (status == 1) {
			this.collectImageView.setImageResource(R.drawable.collected);
		} else {
			this.collectImageView.setImageResource(R.drawable.home_collection);
		}

	}

	public class HeaderViewHolder extends RecyclerView.ViewHolder {
		ImageView iv_hairstyle;
		ImageView iv_headimg;
		ImageView iv_share;
		Button iv_appointment;
		ImageView iv_back;
		ImageView iv_collection;
		TextView tv_style_intro;
		TextView tv_name;
		TextView tv_post;
		TextView tv_words;
		TextView tv_collections;
		TextView tv_orders;
		TextView tv_type;
		TextView tv_price;

		// Button btn_coupon;
		// RatingBar ratingBar1;

		public HeaderViewHolder(View itemView) {
			super(itemView);
			this.tv_style_intro = (TextView) itemView
					.findViewById(R.id.tv_style_intro);
			this.tv_name = (TextView) itemView.findViewById(R.id.tv_name);
			this.tv_post = (TextView) itemView.findViewById(R.id.tv_post);
			this.tv_words = (TextView) itemView.findViewById(R.id.tv_words);
			this.tv_collections = (TextView) itemView
					.findViewById(R.id.tv_collections);
			this.tv_orders = (TextView) itemView.findViewById(R.id.tv_orders);
			this.tv_type = (TextView) itemView.findViewById(R.id.tv_type);
			this.tv_price = (TextView) itemView.findViewById(R.id.tv_price);
			this.iv_hairstyle = (ImageView) itemView
					.findViewById(R.id.iv_hairstyle);
			this.iv_headimg = (ImageView) itemView
					.findViewById(R.id.iv_headimg);
			this.iv_share = (ImageView) itemView.findViewById(R.id.iv_share);
			this.iv_appointment = (Button) itemView
					.findViewById(R.id.iv_appointment);
			this.iv_back = (ImageView) itemView.findViewById(R.id.iv_back);
			this.iv_collection = (ImageView) itemView
					.findViewById(R.id.iv_collection);
			// this.btn_coupon = (Button)
			// itemView.findViewById(R.id.btn_coupon);
			// this.ratingBar1 = (RatingBar) itemView
			// .findViewById(R.id.ratingBar1);
			iv_back.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mOnHairStyleClickListener.onBackClick();
				}
			});
			iv_collection.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mOnHairStyleClickListener.onCollectionClick(
							HairStyleAdapter.this.id,
							HairStyleAdapter.this.isCollected);
				}
			});
			iv_appointment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mOnHairStyleClickListener
							.onAppointmentClick(HairStyleAdapter.this.id);
				}
			});
			iv_share.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mOnHairStyleClickListener
							.onShareClick(HairStyleAdapter.this.id,HairStyleAdapter.this.mainPhoto);
				}
			});
			// btn_coupon.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// try {
			// mOnHairStyleClickListener
			// .onCouponClick(HairStyleAdapter.this.coupon
			// .getString("id"));
			// } catch (JSONException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			// }
			// });
		}

	}

	public class RecyclerViewHolder extends RecyclerView.ViewHolder {

		ImageView iv_hairstyle;

		public RecyclerViewHolder(View view) {
			super(view);
			// implementing onClickListener
			// iv_hairstyle = (ImageView) view.findViewById(R.id.iv_hairstyle);
			iv_hairstyle = (ImageView) view.findViewById(R.id.iv_style_photo);

		}
	}

}
